package com.example.sample.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.R.layout;

public class SpannableActivity extends BaseActivity {
	
	private TextView text;
	private TextView text2;
	private View btn;
	private SpannableString spannableString;
	private SpannableString spannableString2;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spannable);
		text = (TextView)findViewById(R.id.text);
		text2 = (TextView)findViewById(R.id.text2);
		
		spannableString = new SpannableString("spannable");
		text.setText(spannableString);
		
		spannableString2 = new SpannableString("spannable");
		text2.setText(spannableString2);
		
		btn = findViewById(R.id.btn);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				doIt();
			}
		});
	};
	
	private void doIt(){
		
		Uri uri = Uri.parse("https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=3607673210,3300275887&fm=96&s=587263DB043A5F960285ED6B0300C056");
		Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
		ImageSpan imageSpan = new ImageSpan(bmp);
		spannableString.setSpan(imageSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		text.setText(spannableString);
		
		ImageSpan imageSpan2 = new ImageSpan(bmp);
		spannableString2.setSpan(imageSpan2, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		text2.setText(spannableString2);

		TextView tv = new TextView(this);
		tv.setBackgroundColor(Color.BLUE);
		tv.setTextSize(30);
		tv.setText("隆\n盛\n科\n技\n打\n发\n时\n间\n的\n法\n律\n是\n看\n得\n见\n风\n啊\n是\n看\n得\n见\n风\n的\n看\n得\n见\n风");
		PopupWindow pw = new PopupWindow(tv,  -1, -1, true);

		pw.setContentView(tv);
		pw.setTouchable(true);
		pw.setOutsideTouchable(true);
		pw.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));

		//pw.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM|Gravity.LEFT, 0, 0);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			//pw.showAsDropDown(btn);

			pw.setClippingEnabled(false);
			pw.showAtLocation(getWindow().getDecorView(), Gravity.TOP, 0, 500);
		}
	}
}
